﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Configuration.cs" company="Data Ductus Malmö">
//   (c) 2010 - 2012 Data Ductus Malmö AB
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace DataDuctus.Snmp.Persistor.Core.MetaData
{
  using System;
  using System.Linq;
  using System.Reflection;

  using DataDuctus.Snmp.Persistor.Core.Attributes;
  using DataDuctus.Snmp.Persistor.Core.MetaData.Extensions;

  /// <summary>
  /// The configuration.
  /// </summary>
  public class Configuration
  {
    #region Constructors and Destructors

    /// <summary>
    /// Initializes a new instance of the <see cref="Configuration"/> class.
    /// </summary>
    public Configuration()
    {
      this.Repository = new MetaRepository();
    }

    #endregion

    #region Public Properties

    /// <summary>
    /// Gets the repository.
    /// </summary>
    public MetaRepository Repository { get; private set; }

    #endregion

    #region Public Methods and Operators

    /// <summary>
    /// The add.
    /// </summary>
    /// <param name="assembly">
    /// The assembly.
    /// </param>
    /// <returns>
    /// The SnmpPersistor.Core.MetaData.Configuration.
    /// </returns>
    public Configuration Add(Assembly assembly)
    {
      foreach (
        Type entity in from type in assembly.GetTypes() where type.HasAttribute<EntityAttribute>(true) select type)
      {
        this.Repository.Add(entity);
      }

      return this;
    }

    /// <summary>
    /// The add.
    /// </summary>
    /// <param name="entity">
    /// The entity.
    /// </param>
    /// <returns>
    /// The SnmpPersistor.Core.MetaData.Configuration.
    /// </returns>
    public Configuration Add(Type entity)
    {
      this.Repository.Add(entity);

      return this;
    }

    /// <summary>
    /// The add.
    /// </summary>
    /// <param name="entity">
    /// The entity.
    /// </param>
    /// <returns>
    /// The SnmpPersistor.Core.MetaData.Configuration.
    /// </returns>
    public Configuration Add(string entity)
    {
      this.Repository.Add(Type.GetType(entity));

      return this;
    }

    #endregion
  }
}